{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>tid</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>71535643</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>71535643</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>71535643</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>71535643</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>71535643</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   userId       tid  score\n",
       "0       1  71535643    5.0\n",
       "1       2  71535643    5.0\n",
       "2       3  71535643    5.0\n",
       "3       4  71535643    5.0\n",
       "4       5  71535643    5.0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "#读取数据\n",
    "df = pd.read_csv('C:\\\\Users\\\\17914\\\\Desktop\\\\travelCF\\\\static\\\\new_data.csv') \n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "target_user = 4\n",
    "target_user_data = df[df['userId'] == target_user]\n",
    "print(target_user)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tid     75611      75613      75615      75618      75620      75624       \n",
      "userId                                                                     \n",
      "1             0.0        0.0        0.0        0.0        0.0        0.0  \\\n",
      "2             0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "3             0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "4             0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "5             0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "...           ...        ...        ...        ...        ...        ...   \n",
      "31983         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31984         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31985         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31986         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31987         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "\n",
      "tid     75627      76144      76154      80267      ...  24301253   25276237    \n",
      "userId                                              ...                         \n",
      "1             0.0        0.0        0.0        0.0  ...        0.0        0.0  \\\n",
      "2             0.0        0.0        0.0        0.0  ...        0.0        0.0   \n",
      "3             0.0        0.0        0.0        0.0  ...        0.0        0.0   \n",
      "4             0.0        0.0        0.0        0.0  ...        0.0        0.0   \n",
      "5             0.0        0.0        0.0        0.0  ...        0.0        0.0   \n",
      "...           ...        ...        ...        ...  ...        ...        ...   \n",
      "31983         0.0        0.0        0.0        0.0  ...        2.0        0.0   \n",
      "31984         0.0        0.0        0.0        0.0  ...        1.0        0.0   \n",
      "31985         0.0        0.0        0.0        0.0  ...        1.0        0.0   \n",
      "31986         0.0        0.0        0.0        0.0  ...        1.0        0.0   \n",
      "31987         0.0        0.0        0.0        0.0  ...        1.0        0.0   \n",
      "\n",
      "tid     29950533   31660922   31678859   33765359   52075446   71535643    \n",
      "userId                                                                     \n",
      "1             0.0        0.0        0.0        0.0        0.0        5.0  \\\n",
      "2             0.0        0.0        0.0        0.0        5.0        5.0   \n",
      "3             0.0        0.0        0.0        0.0        0.0        5.0   \n",
      "4             0.0        0.0        0.0        0.0        0.0        5.0   \n",
      "5             0.0        0.0        0.0        0.0        0.0        5.0   \n",
      "...           ...        ...        ...        ...        ...        ...   \n",
      "31983         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31984         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31985         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31986         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "31987         0.0        0.0        0.0        0.0        0.0        0.0   \n",
      "\n",
      "tid     124908212  140068129  \n",
      "userId                        \n",
      "1               0        0.0  \n",
      "2               0        0.0  \n",
      "3               0        0.0  \n",
      "4               0        0.0  \n",
      "5               0        0.0  \n",
      "...           ...        ...  \n",
      "31983           0        0.0  \n",
      "31984           0        0.0  \n",
      "31985           0        0.0  \n",
      "31986           0        0.0  \n",
      "31987           0        0.0  \n",
      "\n",
      "[31987 rows x 35 columns]\n"
     ]
    }
   ],
   "source": [
    "user_item_matrix = df.pivot_table(index='userId',columns='tid',values='score',fill_value=0)\n",
    "print(user_item_matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "KNeighborsClassifier()\n"
     ]
    }
   ],
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=5)\n",
    "knn.fit(user_item_matrix, user_item_matrix.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
      " 0. 0. 0. 0. 0. 0. 0. 0. 5. 0. 0.]\n",
      "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
      "  0. 0. 0. 0. 0. 0. 0. 0. 5. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "target_user_vector = user_item_matrix.loc[target_user].values\n",
    "target_user_vector = target_user_vector.reshape(1, -1)\n",
    "print(target_user_vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[25  8 14  4 20 19 16  3 39 36  5 24 33 15  2 21 41 26 28 40]]\n"
     ]
    }
   ],
   "source": [
    "recommendations = knn.kneighbors(target_user_vector, 20, return_distance=False)\n",
    "print(recommendations)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-29 06:08:18.563544\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime, timedelta\n",
    "current_time = datetime.now()\n",
    "start_time = current_time - timedelta(days=4) + timedelta(hours=7)\n",
    "\n",
    "print(start_time)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
